package com.lut.reservation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.lut.reservation.bean.Result;
import com.lut.reservation.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: zyt
 */
@Slf4j
public class BaseController<S extends IService<T>, T> {

  @Autowired(required = false)
  protected S service;

  @PostMapping
  public Result<Void> insert(@RequestBody T bean) {
    service.save(bean);
    return ResultUtil.success();
  }

  @GetMapping("/{id}")
  public Result<T> getById(@PathVariable Integer id) {
    final T bean = service.getById(id);
    return ResultUtil.success(bean);
  }

  @PutMapping("/{id}")
  public Result<Void> update(@RequestBody T bean) {
    log.info("更新菜单:{}", bean);
    service.updateById(bean);
    return ResultUtil.success();
  }

  @PostMapping("/page")
  public Result<Page<T>> page(Integer pageSize, Integer pageNum, @RequestParam Map<String, String> query) {
    final Page<T> page = service.page(new Page<>(pageNum, pageSize), queryWrapper(query));
    return ResultUtil.success(page);
  }

  @DeleteMapping("/{id}")
  public Result<Void> del(@PathVariable Integer id) {
    log.info("删除:{}", id);
    service.removeById(id);
    return ResultUtil.success();
  }

  @DeleteMapping
  public Result<Void> del(@RequestParam("id") List<String> ids) {
    log.info("批量删除:{}", ids);
    service.removeByIds(ids);
    return ResultUtil.success();
  }


  public QueryWrapper<T> queryWrapper(Map<String, String> map) {
    log.info("query map:{}", map);
    return new QueryWrapper<>();
  }

}
